﻿package {
	
	import flash.display.*;
	import flash.events.*;
	
	import lt.uza.utils.*;
	import Gene;
	
	
	public class Titan extends MovieClip {
		//VARS
		
		private var global:Global = Global.getInstance();
		public var placeholder_chromosome:Array= new Array();
		var vector: Array =[0,0];
		var destination: Array =[0,0];
		var ID:int;
		var Idle: Boolean = true;
		var Selected:Boolean = false;
		
		var Visible=true;
		
		var placeholder: Shape= new Shape();
				
		var r:uint = 100+Math.random()*155
		var g:uint = 100+Math.random()*155
		var B:uint = 100+Math.random()*155

		public function Titan():void{
			//INIT
						
			placeholder.graphics.beginFill(r<<16 | g<<8 | B);
			placeholder.graphics.drawCircle(0, 0, 10);
			
			Idle=true;
			
			this.addChild(placeholder);
			
			x=300+Math.random()*-250;
			y=300+Math.random()*-250;

		}
		
		private function Move():void{
			x+=vector[0];
			y+=vector[1];
			
		}
		public function setChromosomes():void{
			var u:uint= Math.abs(Math.random()*global.chromosomes.genes.length);
			//TODO CHROMOSOME PAIRS
			placeholder_chromosome.push(global.chromosomes.genes[int(Math.random()*global.chromosomes.genes.length)]);
			placeholder_chromosome.push(global.chromosomes.genes[int(Math.random()*global.chromosomes.genes.length)]);
			//trace(placeholder_chromosome);
		}
		public function Goto(xx:int,yy:int):void{
			
			destination[0] = xx ;
			destination[1] = yy ;
			UpdateVector();
			
		}
		public function UpdateVector():void{
			var a = destination[0]-x;
			var b = destination[1]-y;
			var len=Math.sqrt(a*a+b*b);			
			vector[0]=a/len;
			vector[1]=b/len;
		}
		public function Update():void{

			x+= global.camera._x;
			y+= global.camera._y;
			
			destination[0]+= global.camera._x;
			destination[1]+= global.camera._y;
			
			
			if((Math.abs(destination[0]-x)>=10 || Math.abs(destination[1]-y)>=10)&&!Idle){
				Move();
				Idle = false;
			}else{
				Idle = true;
			}
			
			if(Selected){
				placeholder.scaleX=placeholder.scaleY=2;
				
			}else{
				placeholder.scaleX=placeholder.scaleY=1;
			}
			
			Visible=true;
			if(x>global.stageWidth){
				Visible=false;
				//placeholder_OffScreen.x=global.stageWidth/2-10;
				
			}
			if(x<0){
				Visible=false;
				//placeholder_OffScreen.x=10;
			}
			if(y>global.stageHeight){
				Visible=false;
				
				//placeholder_OffScreen.y=global.stageHeight-10;
			}
			if(y<0){
				Visible=false;
				
				//placeholder_OffScreen.y=10;
			}
			if(!Visible){
				placeholder.visible=false;
			}else{
				placeholder.visible=true;
			}
			
		}
				
	}
}